import router from '../../router'

const USER_INIT = 'USER_INIT'
const USER_REMOVE = 'USER_REMOVE'
const USER_CHANGE = 'USER_CHANGE'
const USER_ADD = 'USER_ADD'
const LOGINUSER_CHANGE = 'LOGINUSER_CHANGE'

function createBooks() {
  let cid = 0
  let books = [
    {name: '妈妈', tel: 1234555656, status: "亲人"},
    {name: 'Nic', tel: 1234555656, status: "朋友"},
    {name: '爸爸', tel: 1234555656, status: "亲人"},
    {name: 'Wind', tel: 1234555656, status: "朋友"},
    {name: 'Lily', tel: 1234555656, status: "同学"},
    {name: '爷爷', tel: 1234555656, status: "亲人"},
    {name: 'Tom', tel: 1234555656, status: "同学"},
    {name: 'Jack', tel: 1234555656, status: "同学"},
    {name: 'James', tel: 1234555656, status: "同学"},
    {name: 'Kobe', tel: 1234555656, status: "同学"},
    {name: '外婆', tel: 1234555656, status: "亲人"},
    {name: 'Harden', tel: 1234555656, status: "同学"},
    {name: 'O`neal', tel: 1234555656, status: "同学"},
    {name: 'Ross', tel: 1234555656, status: "同学"},
  ]
  books = books.filter((item) => {
    item.id = cid++
    item.imgSrc = '/static/img/userImg.png'
    return item
  })
  return books
}

export default {
  state: {
    personal: {},
    books: {},
    older: {}
  },
  mutations: {
    [USER_INIT](state, info) {
      state.personal = info.personal
      state.books = info.books
      state.older = info.personal
    },
    [USER_REMOVE](state, id) {
      state.books = state.books.filter(item => {
        return item.id != id
      })
      localStorage.books = JSON.stringify(state.books)
    },
    [USER_CHANGE](state, user) {
      state.books.forEach(item => {
        if(item.id == user.id) {
          item.name = user.name
          item.tel = user.tel
        }
      })
      localStorage.books = JSON.stringify(state.books)
    },
    [USER_ADD](state, user) {
      user.id = state.books.length
      user.imgSrc = '/static/img/userImg.png'
      state.books.push(user)
      localStorage.books = JSON.stringify(state.books)
    },
    [LOGINUSER_CHANGE](state, user) {
      state.personal = user
      sessionStorage.setItem('user', JSON.stringify(state.personal))
      localStorage.setItem(sessionStorage.userId, JSON.stringify(user))
    }
  },
  actions: {
    userInit({commit}) {
      if(sessionStorage.login && sessionStorage.login == 1) {
        if(!localStorage.books ||  []) {
          const books = createBooks()
          localStorage.books = JSON.stringify(books)
        }
        const personal = JSON.parse(sessionStorage.user)

        commit('USER_INIT', {
          personal,
          books: JSON.parse(localStorage.books)
        })
      } else {
        alert('请先登录！')
        router.replace('/home/login')
      }
    },
    userRemove({commit}, userId) {
      commit('USER_REMOVE', userId)
    },
    userChange({commit}, user) {
      commit('USER_CHANGE', user)
    },
    userAdd({commit}, user) {
      commit('USER_ADD', user)
    },
    loginUserChange({commit}, user) {
      commit('LOGINUSER_CHANGE', user)
    }
  }
}